package com.controller.base;

import java.beans.PropertyEditor;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver;

import com.service.SysManager;
import com.vo.Site;

public class MultiMethodController extends MultiActionController implements
		InitializingBean {
	public final static int pageSize=20;
	public final static int pageNo=1;
	public static Map select=new HashMap();//j_code常用信息一次放到内存中，第二次调用就不用到数据库中查询
	private Properties urlMethodmappings;
	/**
	 * @return Returns the urlMethodmappings.
	 */
	public Properties getUrlMethodmappings() {
		return urlMethodmappings;
	}

	/**
	 * @param urlMethodmappings
	 *            The urlMethodmappings to set.
	 */
	public void setUrlMethodmappings(Properties urlMethodmappings) {
		this.urlMethodmappings = urlMethodmappings;
	}

	public void afterPropertiesSet() throws Exception {
		if (urlMethodmappings != null && !urlMethodmappings.isEmpty()) {

			PropertiesMethodNameResolver propertiesMethodNameResolver = new PropertiesMethodNameResolver();
			propertiesMethodNameResolver.setMappings(urlMethodmappings);
			this.setMethodNameResolver(propertiesMethodNameResolver);
			if (this.logger.isInfoEnabled()) {
				this.logger.info("binding success...... ");
			}
		} else {
			logger.info("no 'urlMethodmappings' set on MultiMethodController");
		}
	}

	public void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		PropertyEditor booleanEditor = new CustomBooleanEditor(true);
		binder.registerCustomEditor(boolean.class, booleanEditor);
		binder.registerCustomEditor(Boolean.class, booleanEditor);
		binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(
				BigDecimal.class, true));
		binder.registerCustomEditor(BigInteger.class, new CustomNumberEditor(
				BigInteger.class, true));
		// DateFormat dateFormat = DateFormat.getInstance();
		//初始化数据
		
//		Site site=Site.getInstance();
//		org.springframework.web.context.WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
//		SysManager sys=(SysManager)wac.getBean("sysManager");
//		site.setMenu(sys.getMenuDao().getMenuList());
//		site.init();
//		request.getSession().getServletContext().setAttribute("site", site);
	}

	public ModelAndView getMessage(HttpServletResponse response, String message) {
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			out.write(message);
			out.flush();
			out.close();
		} catch (Exception e) {
			if (out != null) {
				out.close();
			}
		}
		return null;
	}

	public Object getObjectFromJson(HttpServletRequest request,
			HttpServletResponse response, java.io.Serializable obj)
			throws Exception {
		logger.info(request.getRequestURI());
		request.setCharacterEncoding("utf-8");
		Enumeration en = request.getParameterNames();
		String json = "";
		for (; en.hasMoreElements();) {
			String k = (String) en.nextElement();
			String v = request.getParameter(k);
			if (v != null)
				json += "\""+k + "\":\"" + v.trim() + "\",";
			else
				json += "\""+k + "\":\"\",";
		}
		json="{"+json+"}";
		logger.info(json);
		JSONObject jsonObj = JSONObject.fromObject(json);
		Object object = JSONObject.toBean(jsonObj, Class.forName(obj.toString()));
		logger.info(object);
		return object;
	}

	public String getJsonFromJSONArray(Object map) {
		JSONArray jsonObject = JSONArray.fromObject(map);
		String json = jsonObject.toString();
		logger.info(json);
		return json;
	}

	public String getJsonFromJSONObject(Object map) {
		JSONObject jsonObject = JSONObject.fromObject(map);
		String json = jsonObject.toString();
		logger.info(json);
		return json;
	}
	public String getJscriptAlert(String msg){
		return "<script>alert('"+msg+"');</script>";
	}
}
